<?php

include "Connect.php";

if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;
$max_results = 4;
$from = (($page * $max_results) - $max_results);
?>
<?php
$sql = "SELECT * FROM food "." LIMIT $from, $max_results";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs, MYSQL_ASSOC)){

    echo $row['ID'] . " " . $row['Name'] . " ". $row['Price']. "<br />";
}

// (1) get the total number of results for your query
// modify this to match the total results for the main query
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM food"),0); 

// (2) Calculate total number of pages. Round up using ceil()
$total_pages = ceil($total_results / $max_results); 

if($total_results > $max_results){
  // (3) build Previous link
  if($page > 1){
     $prev = ($page - 1);
     echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">&lt;&lt; Previous</a> ";
  } 

  // (4) display page numbers
  for($i = 1; $i <= $total_pages; $i++){
     if($page == $i){
        echo $i . " ";
     }
     else{
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
     }
  } 

  // (5) build Next Link
  if($page < $total_pages){
       $next = ($page + 1);
       echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next &gt;&gt;</a>";
  }
}
?>
